package cn.itcast.bos.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;



@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends BaseAction<Courier>{

	@Autowired
	private CourierService courierService;
	
	@Action(value="courierSave" ,results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String save(){
		
		courierService.save(model);
		return SUCCESS;
	}

	@Action(value="queryAll",results={@Result(name="success",type="json")})
	public String queryAll(){
		Pageable pageable = new PageRequest(page-1, rows);
		
		// 封装条件查询对象 Specification
				Specification<Courier> specification = new Specification<Courier>() {
					@Override
					// Root 用于获取属性字段，CriteriaQuery可以用于简单条件查询，CriteriaBuilder 用于构造复杂条件查询
					public Predicate toPredicate(Root<Courier> root,
							CriteriaQuery<?> query, CriteriaBuilder cb) {
						List<Predicate> list = new ArrayList<Predicate>();

						// 简单单表查询
						if (StringUtils.isNotBlank(model.getCourierNum())) {
							Predicate p1 = cb.equal(
									root.get("courierNum").as(String.class),
									model.getCourierNum());
							list.add(p1);
						}
						if (StringUtils.isNotBlank(model.getCompany())) {
							Predicate p2 = cb.like(
									root.get("company").as(String.class),
									"%" + model.getCompany() + "%");
							list.add(p2);
						}
						if (StringUtils.isNotBlank(model.getType())) {
							Predicate p3 = cb.equal(root.get("type").as(String.class),
									model.getType());
							list.add(p3);
						}
				
						if (model.getStandard() != null&& StringUtils.isNotBlank(model.getStandard()
								.getName())) {
							// 多表查询
							Join<Courier, Standard> standardJoin = root.join("standard",
									JoinType.INNER);	
							
							Predicate p4 = cb.like(
									standardJoin.get("name").as(String.class), "%"
											+ model.getStandard().getName() + "%");
							list.add(p4);
						}
						return cb.and(list.toArray(new Predicate[0]));
					}
				};
		
		Page<Courier> page = courierService.findPageData(specification,pageable);
		pushPageDataToValueStack(page);
		return SUCCESS;
	}
	private String ids;
	
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	@Action(value="courierDel",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String courierDel(){
		String[] strings = ids.split(",");
		courierService.courierDel(strings);
		return SUCCESS;
	}
	
	@Action(value="courierRes",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String courierRes(){
		String[] strings = ids.split(",");
		courierService.courierRes(strings);
		return SUCCESS;
	}
	
	@Action(value="getCourier",results={@Result(name="success",type="json")})
	public String getCourier(){
		
		List<Courier> list = courierService.getCourier();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
